<template>
  <div class="menu">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button @click="$router.push({ name: 'advert-create' })">添加广告</el-button>
      </div>
      <el-table
        :data="list"
        style="width: 100%">
        <el-table-column
          label="id"
          min-width="150"
          type="index">
        </el-table-column>
        <el-table-column
          prop="name"
          label="广告名称"
          min-width="150">
        </el-table-column>
        <el-table-column
          prop="name"
          label="广告图片"
          min-width="200">
          <template slot-scope="scope">
            <img :src="scope.row.img" height="80" alt="">
          </template>
        </el-table-column>
        <el-table-column
          label="时间"
          min-width="200">
          <template slot-scope="scope">
            <p>开始时间：{{scope.row.startTime}}</p>
            <p>到期时间：{{scope.row.endTime}}</p>
          </template>
        </el-table-column>
        <el-table-column
          prop="orderNum"
          label="上线/下线"
          min-width="150">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              :disabled="scope.row.isStatusLoading"
              @change="onStateChange(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          min-width="150">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getAllAdvert, changeState } from '@/services/advert'

export default Vue.extend({
  name: 'advertIndex',
  data () {
    return {
      list: [] // 菜单列表
    }
  },

  created () {
    this.loadAllAdvert()
  },

  methods: {
    async loadAllAdvert () {
      const { data } = await getAllAdvert()
      this.list = data.content
    },

    handleEdit (item: any) {
      this.$router.push({
        name: 'advert-edit',
        params: {
          id: item.id
        }
      })
    },

    async onStateChange (course: any) {
      course.isStatusLoading = true
      await changeState({
        id: course.id,
        status: course.status
      })
      this.$message.success(`${course.status === 0 ? '下线' : '上线'}成功`)
      course.isStatusLoading = false
    }
  }
})
</script>

<style lang="scss" scoped></style>
